import regeneratorRuntime from '../../../resource/libs/runtime'
import service from '../../../common/service'

Page({
  data: {
    if_no_more: false,
    if_loading: false,
    list: [],
    page: 0
  },

  onShow () {
    this.setData({
      if_no_more: false,
      list: [],
      page: 0
    }, () => {
      this.getList()
    })
  },

  onPullDownRefresh () {
    this.setData({
      if_no_more: false,
      list: [],
      page: 0
    }, () => {
      this.getList()
      wx.stopPullDownRefresh()
    })
  },

  async getList () {
    if (this.data.if_no_more || this.data.if_loading) {
      return
    }
    this.setData({ if_loading: true })
    let page = this.data.page
    let res = await service.getPromise('orders/mini', {
      page: page + 1,
      size: 6
    })
    this.setData({ if_loading: false })
    if (res && typeof res !== 'number') {
      let data = res.data.data
      this.setData({
        ['list[' + page + ']']: data,
        if_no_more: data.length ? false : true,
        page: page + 1
      })
    }
  }
})